package com.czy.service.impl;

import com.czy.common.Result;
import com.czy.dao.AccountDao;
import com.czy.dao.UserInfoDao;
import com.czy.model.Account;
import com.czy.model.UserInfo;
import com.czy.service.AccountService;
import com.czy.util.DateUtil;
import com.czy.util.EmailUtil;
import com.czy.util.RedisTokenManger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.UUID;

/**
 * @Author: czy
 * @Date: 2019/11/8 13:53
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private RedisTokenManger redisTokenManger;

    /**
     * 登录
     * @param userName
     * @param password
     * @return
     */
    @Override
    public Result login(String userName, String password) {
        Account account=accountDao.selectByAccountName(userName);
        if (account==null){
            return Result.fail("用户名或密码错误");
        }
        if ("0".equals(account.getFlag())){
            return Result.fail("用户未激活,请在注册邮箱激活");
        }
        if ("-1".equals(account.getFlag())){
            return Result.fail("账号存在异常,请联系管理员");
        }
        UserInfo userInfo = userInfoDao.selectById(account.getUserId());
        String token = redisTokenManger.getToken(userInfo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("userId",userInfo.getUserId());
        map.put("name",userInfo.getName());
        return Result.success(map,"登录成功");
    }

    /**
     * 添加用户
     * @param account
     * @return
     */
    @Transactional
    @Override
    public Result insert(Account account) {
        //添加用户信息
        UserInfo userInfo = new UserInfo();
        userInfo.setName(account.getName());
        userInfo.setEmail(account.getEmail());
        int m = userInfoDao.insert(userInfo);
        if (m<=0){
            return Result.fail("添加失败");
        }
        //添加账号
        //生成激活码
        String activiteCode = UUID.randomUUID().toString().replace("-", "");
        account.setActiviteCode(activiteCode);
        account.setUserId(userInfo.getUserId());
        account.setCreateDate(DateUtil.getDate());
        account.setFlag("0");
        int insert = accountDao.insert(account);
        if (insert<=0 || m<=0){
            return Result.fail("添加失败");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("key",account.getAccountId());
        //发送激活邮件
        emailUtil.sendHtmlMail(account.getEmail(),activiteCode);
        return Result.success(map,"添加成功");
    }

    /**
     *
     * @param activiteCode
     * @return
     */
    @Override
    public Integer findAccountByActivitieCode(String activiteCode) {
        return accountDao.findAccountByActivitieCode(activiteCode);
    }

    @Override
    public Integer updateAccountById(Integer userId) {
        return accountDao.updateAccountFlagById(userId);
    }


    /**
     * 获取当前登录的用户
     * @param
     * @return
     */
    @Override
    public UserInfo getCurrentUser(){
        String token = getToken();
        UserInfo userInfo = (UserInfo)redisTokenManger.getUserInfoByToken(token);
        return userInfo;
    }

    /**
     * 获取当前登录的token
     * @return
     */
    public String getToken(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("Authorization");
        return token;
    }
}
